//
//  MultipleTabView.m
//  TengyunStation
//
//  Created by Johnny on 15/8/27.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "MultipleTabView.h"
#import "TSHeader.h"
#define ButtonTag   100

@implementation MultipleTabView

- (void)awakeFromNib {
    self.backgroundColor = [UIColor whiteColor];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

- (void)setTabArray:(NSArray *)tabArray {
    _tabArray = tabArray;
    _buttonWidth = self.frame.size.width/tabArray.count;
    for (int i = 0; i < tabArray.count; i ++) {
        NSString *title = [tabArray objectAtIndex:i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(_buttonWidth * i, 0, _buttonWidth, self.frame.size.height);
        [button setBackgroundColor:[UIColor clearColor]];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:AssistTextColor forState:UIControlStateNormal];
        [button setTitleColor:OrangeColor forState:UIControlStateSelected];
        button.tag = ButtonTag + i;
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        if (i == 0) {
            [self setButton:button Selected:YES];
        }
    }
    NSString *firstTitle = [tabArray firstObject];
    CGSize size = [firstTitle sizeWithFont:[UIFont systemFontOfSize:15]];
    _lineLabel = [[UILabel alloc] initWithFrame:CGRectMake((_buttonWidth - size.width)/2.0, (self.frame.size.height - 5), size.width, 2)];
    _lineLabel.backgroundColor = OrangeColor;
    [self addSubview:_lineLabel];
}

- (void)setIndex:(NSInteger)index {
    _lastInteger = self.index;
    if (index != _lastInteger) {
        _index = index;
        UIButton *lastButton = (UIButton *)[self viewWithTag:_lastInteger + ButtonTag];
        [self setButton:lastButton Selected:NO];
        UIButton *tapButton = (UIButton *)[self viewWithTag:index + ButtonTag];
        CGSize size = [tapButton.titleLabel.text sizeWithFont:[UIFont systemFontOfSize:15]];
        [self setButton:tapButton Selected:YES];
        [UIView animateWithDuration:0.2 animations:^{
            //((width - size.width)/2.0, (self.frame.size.height - 5), size.width, 2)
            _lineLabel.frame = CGRectMake(_buttonWidth * index + ((_buttonWidth - size.width)/2.0), _lineLabel.frame.origin.y, size.width, 3);
        }];
    }
}

- (void)setButton:(UIButton *)button Selected:(BOOL)isSelected {
    button.selected = isSelected;
    button.userInteractionEnabled = !isSelected;
}

#pragma mark 
- (void)buttonAction:(UIButton *)button {
    self.index = button.tag - ButtonTag;
    if ([_delegate respondsToSelector:@selector(buttonClickIndex:)]) {
        [_delegate buttonClickIndex:self.index];
    }
}

- (void)dealloc {
    _tabArray = nil;
}

@end
